package com.mayisoftware.mayioa.business.flow.impl.serviceImpl;

import com.mayisoftware.mayioa.business.flow.api.domain.BaseFlowInfo;
import com.mayisoftware.mayioa.business.personal.api.domain.PersonalSign;
import com.mayisoftware.mayioa.business.personal.api.service.IPersonalSignService;
import com.mayisoftware.mayioa.common.impl.utils.bean.ReflectionUtil;
import com.mayisoftware.mayioa.platform.api.domain.SysUser;
import com.mayisoftware.mayioa.platform.api.service.ISysMailService;
import com.mayisoftware.mayioa.platform.api.service.ISysUserService;
import com.mayisoftware.mayioa.platform.impl.util.SpringUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;

/**
 * 报名审核成功
 */
public class SendSuccEmailSystemdelegate implements  JavaDelegate  {
    /**
     * 日志类
     */
    private static final Logger log = LoggerFactory.getLogger(SendSuccEmailSystemdelegate.class);

    @Override
    public void execute(DelegateExecution delegateExecution) {
        log.warn("通用的系统报名成功发送邮件开始执行");

        //校验通过后设置outcome
        delegateExecution.setVariable("flowName",delegateExecution.getCurrentFlowElement().getName());

        delegateExecution.setVariable("remark","系统报名成功邮件已经发送");
        delegateExecution.setVariable("outcome","通过");

        Field[] fields = ReflectionUtil.getAllDeclareFields(BaseFlowInfo.class);

        for (Field field:fields){
            Object tmpValue =delegateExecution.getVariable(field.getName());
            if (tmpValue == null && field.getName().equalsIgnoreCase("ApprovalUserId")){
                tmpValue = "system";
            }

            if (tmpValue == null && field.getName().equalsIgnoreCase("ApprovalUserName")){
                tmpValue = "system";
            }

            delegateExecution.setVariable(delegateExecution.getCurrentFlowElement().getId()+"|"+field.getName(),tmpValue);
        }

        IPersonalSignService personalSignService= SpringUtils.getBean(IPersonalSignService.class);

        try {
            if (personalSignService != null){
                PersonalSign personalSign = new PersonalSign();
                personalSign.setPerSignPlanId(Integer.parseInt((String)delegateExecution.getVariable("planId")));
                personalSign.setPerSignUserId(Integer.parseInt((String)delegateExecution.getVariable("applyUserId")));
                personalSign.setResult("1");
                personalSign.setProcessId(delegateExecution.getProcessInstanceId());
                personalSignService.updateUser(personalSign);
            }
            else {
                log.error("无法刷新报名成功结果");
            }
        }
        catch (Exception e){
            log.error("无法刷新报名成功结果",e);
        }

        ISysMailService sysMailService= SpringUtils.getBean(ISysMailService.class);
        if(null == sysMailService){
            log.error("邮件服务读取失败");
            return;
        }

        ISysUserService sysUserService = SpringUtils.getBean(ISysUserService.class);
        if(null == sysUserService){
            log.error("用户服务读取失败");
            return;
        }


        String userId = (String)delegateExecution.getVariable("applyUserId");

        try{
            SysUser sysUser= sysUserService.selectUserById(Long.parseLong(userId));
            if (sysUser == null || sysUser.getEmail() == null){
                log.error("邮件地址不存在或者邮件地址无效");
                return;
            }

            String planName = (String)delegateExecution.getVariable("planName");
            String title = "【mayioa系统通知】您在计划"+planName+"的报名申请已经审核通过,请及时参加!";
            String content = "恭喜你通过了"+planName+"报名资格审查，请登录mayioa系统查看计划详细安排.";
            sysMailService.insertMail(sysUser.getEmail(),title,content);
        }
        catch (Exception e){
            log.error("发送邮件异常",e);
        }

    }
}
